package com.happyass.service.impl;

import com.happyass.dao.EntertainmentDao;
import com.happyass.dao.FacilityDao;
import com.happyass.pojo.Entertainment;
import com.happyass.pojo.Facility;
import com.happyass.service.ExcelService;
import com.happyass.show.dao.ActivityDao;
import com.happyass.show.dao.PlanDao;
import com.happyass.show.entity.Activity;
import com.happyass.show.entity.Plan;
import com.happyass.show.entity.PlanActRel;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class ExcelServiceImpl implements ExcelService {
    @Autowired
    EntertainmentDao entertainmentDao;
    @Autowired
    FacilityDao facilityDao;
    @Autowired
    ActivityDao activityDao;
    @Autowired
    PlanDao planDao;

    @Override
    public void excel(HttpServletResponse response,Integer a) throws IOException {
        if (a==1){
            //要导出的数据
            List<Entertainment> list = entertainmentDao.findAll();
            // 创建HSSFWorkbook对象
            HSSFWorkbook wb = new HSSFWorkbook();
            // 创建HSSFSheet对象,如果要创建多个sheet，就再创建sheet对象
            HSSFSheet sheet = wb.createSheet("主题表");
            // 创建HSSFRow对象，先写入列名
            HSSFRow colName = sheet.createRow(0);
            String[] title = {"ID", "主题名", "主题简介", "包含设施"};
            for (int i = 0; i < title.length; i++) {
                colName.createCell(i).setCellValue(title[i]);
            }
            for (int i = 1; i <= list.size(); i++) {
                //从第二行开始写入数据
                HSSFRow row = sheet.createRow(i);
                row.createCell(0).setCellValue(list.get(i - 1).getEntertainmentId());
                row.createCell(1).setCellValue(list.get(i - 1).getEntertainmentName());
                row.createCell(2).setCellValue(list.get(i - 1).getEntertainmentContent());
                String FacilityName = "";
                if (list.get(i - 1).getFacilityList() != null) {
                    for (Facility r : list.get(i - 1).getFacilityList()) {
                        FacilityName += r.getFacilityName() + ",";
                    }
                }
                row.createCell(3).setCellValue(FacilityName);
            }
            String fileName="主题表";
            //解决中文文件名下载 变成下划线的问题
            fileName=new String(fileName.getBytes("utf-8"),"ISO8859-1")+"";
            //设置响应方式
            response.reset();
            //设置响应文件名
            response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");
            //设置下载文件类型
            response.setContentType("application/msexcel");
            //写入你要下的文件
            wb.write(response.getOutputStream());
        }else {
            //要导出的数据
            List<Facility> list = facilityDao.findAll();
            // 创建HSSFWorkbook对象
            HSSFWorkbook wb = new HSSFWorkbook();
            // 创建HSSFSheet对象,如果要创建多个sheet，就再创建sheet对象
            HSSFSheet sheet = wb.createSheet("设施表");
            // 创建HSSFRow对象，先写入列名
            HSSFRow colName = sheet.createRow(0);
            String[] title = {"ID", "设施名", "设施简介", "点赞数", "所属主题", "状态"};
            for (int i = 0; i < title.length; i++) {
                colName.createCell(i).setCellValue(title[i]);
            }
            for (int i = 1; i <= list.size(); i++) {
                //从第二行开始写入数据
                HSSFRow row = sheet.createRow(i);
                row.createCell(0).setCellValue(list.get(i - 1).getFacilityId());
                row.createCell(1).setCellValue(list.get(i - 1).getFacilityName());
                row.createCell(2).setCellValue(list.get(i - 1).getFacilityContent());
                row.createCell(3).setCellValue(list.get(i - 1).getFacilityGreat());
                row.createCell(4).setCellValue(list.get(i - 1).getEntertainmentList().get(0).getEntertainmentName());
                if (list.get(i - 1).getFacilityStatus()==0){
                    row.createCell(5).setCellValue("停用");
                }else if (list.get(i - 1).getFacilityStatus()==1){
                    row.createCell(5).setCellValue("启用");
                }else {
                    row.createCell(5).setCellValue("升级");
                }

            }
            String fileName="设施表";
            //解决中文文件名下载 变成下划线的问题
            fileName=new String(fileName.getBytes("utf-8"),"ISO8859-1")+"";
            //设置响应方式
            response.reset();
            //设置响应文件名
            response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");
            //设置下载文件类型
            response.setContentType("application/msexcel");
            //写入你要下的文件
            wb.write(response.getOutputStream());
        }
    }




    //后台表演节目excel导出
    @Override
    public void activityExcel(HttpServletResponse response, String searchKey, String searchValue) {
        List<Activity> activityList = null;
        String fileName="";
        if (searchKey!=null&&searchValue!=null){
            if (searchKey.equals("activityId")) {
                boolean num = true;
                for (int i = 0; i < searchValue.length(); i++) {
                    if (!Character.isDigit(searchValue.charAt(i))) {
                        num = false;
                        break;
                    }
                }
                if (num) {
                    activityList = activityDao.findByActivityIdLikeAndActivityDel(Integer.parseInt(searchValue), 1);
                } else {
                    activityList = activityDao.findByActivityIdLikeAndActivityDel(999999999, 1);
                }
                fileName="表演节目搜索_节目编号_"+searchValue+"_列表";
            }else if (searchKey.equals("activityName")) {
                activityList = activityDao.findByActivityNameContainingAndActivityDel(searchValue, 1);
                fileName="表演节目搜索_表演节目名_"+searchValue+"_列表";
            } else if (searchKey.equals("activityAddr")) {
                activityList = activityDao.findByActivityAddrContainingAndActivityDel(searchValue, 1);
                fileName="表演节目搜索_所属区域_"+searchValue+"_列表";
            } else if (searchKey.equals("activityArea")) {
                activityList = activityDao.findByActivityAreaContainingAndActivityDel(searchValue, 1);
                fileName="表演节目搜索_表演地点_"+searchValue+"_列表";
            } else if (searchKey.equals("dayTime")) {
                if (searchValue.equals("白天场")){
                    activityList = activityDao.findByDayTimeLikeAndActivityDel(1, 1);
                }else if (searchValue.equals("夜间场")){
                    activityList = activityDao.findByDayTimeLikeAndActivityDel(2, 1);
                }else {
                    activityList = activityDao.findByDayTimeLikeAndActivityDel(9, 1);
                }
                fileName="表演节目搜索_表演场次_"+searchValue+"_列表";
            } else if (searchKey.equals("activityStatus")) {
                if (searchValue.equals("启用")){
                    activityList = activityDao.findByActivityStatusLikeAndActivityDel(1, 1);
                }else if (searchValue.equals("禁用")){
                    activityList = activityDao.findByActivityStatusLikeAndActivityDel(0, 1);
                }else {
                    activityList = activityDao.findByActivityStatusLikeAndActivityDel(9, 1);
                }
                fileName="表演节目搜索_节目状态_"+searchValue+"_列表";
            }
        }else {
            activityList = activityDao.findByActivityDel(1);
            fileName="表演节目列表";
        }

        //操作office文件对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建HSSFSheet对象，如果要创建多个sheet，就再创建sheet对象
        HSSFSheet sheet = workbook.createSheet("表演节目表");
        //创建HSSFRow对象，写入列名
        HSSFRow colName = sheet.createRow(0);
        //定义表头信息
        String[] title = {"节目编号","表演节目名","表演地点","所属区域","表演场次","状态"};
        //在excel中写入表头
        for (int i = 0; i < title.length; i++) {
            colName.createCell(i).setCellValue(title[i]);
        }
        for (int i = 0; i < activityList.size(); i++) {
            HSSFRow row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(activityList.get(i).getActivityId());
            row.createCell(1).setCellValue(activityList.get(i).getActivityName());
            row.createCell(2).setCellValue(activityList.get(i).getActivityArea());
            row.createCell(3).setCellValue(activityList.get(i).getActivityAddr());
            if (activityList.get(i).getDayTime()==1){
                row.createCell(4).setCellValue("白天场");
            }else if (activityList.get(i).getDayTime()==2){
                row.createCell(4).setCellValue("夜间场");
            }
            if (activityList.get(i).getActivityStatus()==1){
                row.createCell(5).setCellValue("启用");
            }else if (activityList.get(i).getActivityStatus()==0){
                row.createCell(5).setCellValue("停用");
            }
        }

        //解决下载文件名中文乱码的问题
        try {
            fileName = new String (fileName.getBytes("UTF-8"),("ISO-8859-1"));
            //设置响应
            response.reset();
            //设置响应文件的名称
            response.setHeader("Content-disposition","attachment; fileName="+fileName+" .xls");
            //设置下载文件类型 excel类型是msexcel
            response.setContentType("application/msexcel");
            //写入要下载的文件
            workbook.write(response.getOutputStream());
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void planExcel(HttpServletResponse response, String planItem) {
        Plan plan = planDao.findByPlanItem(planItem);
        List<PlanActRel> parList = plan.getPlanActRels();
        //操作office文件对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建HSSFSheet对象，如果要创建多个sheet，就再创建sheet对象
        HSSFSheet sheet = workbook.createSheet("表演节目表");
        //创建HSSFRow对象，写入列名
        sheet.createRow(0).createCell(0).setCellValue("白天场9:00-21:00");
        HSSFRow colName1 = sheet.createRow(1);
        //定义表头信息
        String[] title = {"区域","表演地点","表演节目","工作日表演时间","周末表演时间","停演时间"};
        //在excel中写入表头
        for (int i = 0; i < title.length; i++) {
            colName1.createCell(i).setCellValue(title[i]);
        }
        int tableRow = 1;
        for (int i = 0; i < parList.size(); i++) {
            HSSFRow row = sheet.createRow(tableRow+1);
            if (parList.get(i).getActivity().getDayTime()==1){
                row.createCell(0).setCellValue(parList.get(i).getActivity().getActivityAddr());
                row.createCell(1).setCellValue(parList.get(i).getActivity().getActivityArea());
                row.createCell(2).setCellValue(parList.get(i).getActivity().getActivityName());
                if (parList.get(i).getShowWorkdayTime().equals("null")){
                    row.createCell(3).setCellValue("/");
                }else {
                    row.createCell(3).setCellValue(parList.get(i).getShowWorkdayTime());
                }
                if (parList.get(i).getShowWorkdayTime().equals("null")){
                    row.createCell(4).setCellValue("/");
                }else {
                    row.createCell(4).setCellValue(parList.get(i).getShowWeekdayTime());
                }
                if (parList.get(i).getShowStopWeekday().equals("null")){
                    row.createCell(5).setCellValue("/");
                }else {
                    row.createCell(5).setCellValue(parList.get(i).getShowStopWeekday());
                }
                tableRow +=1;
            }
        }

        sheet.createRow(tableRow+1).createCell(0).setCellValue("夜间场17:00-21:00");
        HSSFRow colName2 = sheet.createRow(tableRow+2);
        for (int i = 0; i < title.length; i++) {
            colName2.createCell(i).setCellValue(title[i]);
        }
        for (int i = 0; i < parList.size(); i++) {
            HSSFRow row = sheet.createRow(tableRow+3);
            if (parList.get(i).getActivity().getDayTime()==2){
                row.createCell(0).setCellValue(parList.get(i).getActivity().getActivityAddr());
                row.createCell(1).setCellValue(parList.get(i).getActivity().getActivityArea());
                row.createCell(2).setCellValue(parList.get(i).getActivity().getActivityName());
                if (parList.get(i).getShowWorkdayTime().equals("null")){
                    row.createCell(3).setCellValue("/");
                }else {
                    row.createCell(3).setCellValue(parList.get(i).getShowWorkdayTime());
                }
                if (parList.get(i).getShowWorkdayTime().equals("null")){
                    row.createCell(4).setCellValue("/");
                }else {
                    row.createCell(4).setCellValue(parList.get(i).getShowWeekdayTime());
                }
                if (parList.get(i).getShowStopWeekday().equals("null")){
                    row.createCell(5).setCellValue("/");
                }else {
                    row.createCell(5).setCellValue(parList.get(i).getShowStopWeekday());
                }
                tableRow +=1;
            }
        }

        String fileName = "演出计划表_"+planItem;
        //解决下载文件名中文乱码的问题
        try {
            fileName = new String (fileName.getBytes("UTF-8"),("ISO-8859-1"));
            //设置响应
            response.reset();
            //设置响应文件的名称
            response.setHeader("Content-disposition","attachment; fileName="+fileName+" .xls");
            //设置下载文件类型 excel类型是msexcel
            response.setContentType("application/msexcel");
            //写入要下载的文件
            workbook.write(response.getOutputStream());
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
